﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Threading.Tasks;

namespace Rally_MvvmC.Library
{
    public static class MailService
    {
        public static string GetUrl (Uri requestUri)
        {
            return string.Format("{0}://{1}", requestUri.Scheme, requestUri.Authority);
        }

        public static void SendPasswordResetEmail(Uri requestUri, string resetCode, string toEmail)
        {
            string link = string.Format("{0}/account/resetpassword?resetCode={1}", GetUrl(requestUri), resetCode);
            string subject = "Password Reset";
            string body = string.Format("A request has been received to reset the password for this account.<br/><br/>Please <a href='{0}'>follow this link to reset your password</a> or ignore this email if it wasn't requested by you.", link);
            SendMessage(0, subject, body, toEmail);
        }

        public static void SendMessage(int notification, string subject, string body, string toEmail)
        {
            SendMessage(subject, body, new List<string>() { toEmail });
        }

        public static void SendMessage(string subject, string body, List<string> toEmails)
        {
            foreach (var email in toEmails)
            {
                if (string.IsNullOrWhiteSpace(email))
                {
                    continue;
                }

                MailMessage mailMessage = new MailMessage()
                {
                    Subject = subject,
                    Body = body,
                    IsBodyHtml = true
                };
                    mailMessage.To.Add(new MailAddress(email));
             
                SmtpClient smtpClient = new SmtpClient();
                smtpClient.Send(mailMessage);
            }
        }
    }
}
